package com.dcbeida.activity.clerkmanager;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.Customer;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class AddAndEditClerkPresenter implements AddAndEditClerkContract.Present {


    private AddAndEditClerkContract.View view;

    private ModelUtils model;

    private Activity act;

    public AddAndEditClerkPresenter(AddAndEditClerkContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = ModelUtils.newInstance();
        this.act = act;

    }

    @Override
    public void start() {

    }


    //取消dialog 取消请求
    DialogInterface  .OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    @Override
    public void requestToAdd(Customer customer) {
        model.getDate("createuser",  new JsonGet()
                        .putopt("uid", MyApplication.user.getUid())
                        .putopt("name", customer.getName())
                        .putopt("gender", customer.getGender())
                        .putopt("mobile", customer.getMobile())
                        .putopt("checkintime", customer.getCheckintime())
                        .putopt("job", customer.getJob())
                        .putopt("diploma", customer.getDiploma())
                        .putopt("major", customer.getMajor())
                        .putopt("jobtitle", customer.getJobtitle())
                        .tostring(),
                new CustomerCallback<Object>(act, Object.class, lis));
    }

    @Override
    public void requestToModfiy(Customer customer) {
        model.getDate("modifyuser", new JsonGet()
                        .putopt("uid", MyApplication.user.getUid())
                        .putopt("employeeid", customer.getEmployeeid())
                        .putopt("name", customer.getName())
                        .putopt("gender", customer.getGender())
                        .putopt("mobile", customer.getMobile())
                        .putopt("checkintime", customer.getCheckintime())
                        .putopt("job", customer.getJob())
                        .putopt("diploma", customer.getDiploma())
                        .putopt("major", customer.getMajor())
                        .putopt("jobtitle", customer.getJobtitle())
                        .tostring(),
                new CustomerCallback<Object>(act, Object.class, lis));
    }


    final class CustomerCallback<T> extends DialogCallback<T> {

        public CustomerCallback(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                view.requestToSuccess();
            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("网络异常，请重试");

        }
    }


}
